package com.dmap.knowledge.service.impl;

import com.dmap.knowledge.dao.BannerTagMapper;
import com.dmap.knowledge.po.BannerTag;
import com.dmap.knowledge.po.BannerTagExample;
import com.dmap.knowledge.service.BannerTagService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;


@Service
public class BannerTagServiceImpl implements BannerTagService {
    @Resource
    private BannerTagMapper bannerTagMapper;
    @Override
    public List<BannerTag> selectByBannerId(long id) {
        BannerTagExample bannerTagExample = new BannerTagExample();
        bannerTagExample.createCriteria().andBannerIdEqualTo(id);
        return bannerTagMapper.selectByExample(bannerTagExample);
    }
    @Override
    public int insertBannerTag(long bannerId, long tagTypeId, String tagTypeName, long tagId, String tagName, long createTime) {
        BannerTag bannerTag = new BannerTag();
        bannerTag.setBannerId(bannerId);
        bannerTag.setTagTypeId(tagTypeId);
        bannerTag.setTagId(tagId);
        bannerTag.setTagTypeName(tagTypeName);
        bannerTag.setTagName(tagName);
        bannerTag.setCreateTime(System.currentTimeMillis() / 1000);
        return bannerTagMapper.insertSelective(bannerTag);
    }
    @Override
    public int deleteBannerTagByBannerId(long bannerId) {
        BannerTagExample bannerTagExample = new BannerTagExample();
        bannerTagExample.createCriteria().andBannerIdEqualTo(bannerId);
        //如果没有就返回1
        if (CollectionUtils.isEmpty(bannerTagMapper.selectByExample(bannerTagExample))) {
            return 1;
        }
        return bannerTagMapper.deleteByExample(bannerTagExample);
    }
    @Override
    public int deleteBannerTagList(String bannerIds) {
        List<String> columnidList = Arrays.asList(bannerIds.split(","));
        int count = 0;
        for (String bannerId : columnidList) {
            count += deleteBannerTagByBannerId(Long.parseLong(bannerId));
        }
        if (count == columnidList.size()) {
            return 1;
        }
        return 0;
    }
}
